﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using DataAccess;
namespace SQT.tracnghiem.BaiViet
{
    public partial class NewsCategories : System.Web.UI.Page
    {
        public int page,cate, count =0;
        public string url,CateName;
        public int[] list = new int[4];
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                try
                {
                    cate = int.Parse(RouteData.Values["id"].ToString());
                    page = int.Parse(Request.QueryString["page"].ToString());
                }
                catch
                {
                    cate = int.Parse(RouteData.Values["id"].ToString());
                    page = 1;
                }
                var get_this_cate = new TblNewsCategoryController().FetchAll().Where(x => x.CatId == cate).Single();
                CateName = get_this_cate.CatName;
                url = convertToUrl(convertToUnSign(get_this_cate.CatName));
                var get_top_cate =(from p in new TblNewsController().FetchAll().Where(x => x.NewsCategory == cate && x.NewsDelete == false && x.NewsActive == true && x.NewsTop==true) orderby p.NewsDate descending select p).ToList() ;
                for (int i=0; i<get_top_cate.Count;i++)
                {
                    if (count >= 4)
                        break;
                    list[i] = get_top_cate[i].NewsId;
                    count++;
                }
                if (count < 4)
                {
                    var get_news = (from p in new TblNewsController().FetchAll().Where(x => x.NewsCategory == cate && x.NewsDelete == false && x.NewsActive == true) orderby p.NewsViewed*100/((DateTime.Now.Day - DateTime.Parse(p.NewsDate.ToString()).Day)+1) descending select p).ToList();
                    for (int i = count; i < get_news.Count; i++)
                    {
                        if (count >= 4)
                            break;
                        list[i] = get_news[i].NewsId;
                        count++;
                        
                    }
                }
            }
        }
        public static string convertToUnSign(string s)
        {
            string convert = "ĂÂÀẰẦÁẮẤẢẲẨÃẴẪẠẶẬỄẼỂẺÉÊÈỀẾẸỆÔÒỒƠỜÓỐỚỎỔỞÕỖỠỌỘỢƯÚÙỨỪỦỬŨỮỤỰÌÍỈĨỊỲÝỶỸỴĐăâàằầáắấảẳẩãẵẫạặậễẽểẻéêèềếẹệôòồơờóốớỏổởõỗỡọộợưúùứừủửũữụựìíỉĩịỳýỷỹỵđ\".:;'<>?~!@#$%^&*()_+-=\\/*,";
            string To = "aaaaaaaaaaaaaaaaaeeeeeeeeeeeooooooooooooooooouuuuuuuuuuuiiiiiyyyyydaaaaaaaaaaaaaaaaaeeeeeeeeeeeooooooooooooooooouuuuuuuuuuuiiiiiyyyyyd                           ";
            for (int i = 0; i < To.Length; i++)
            {
                s = s.Replace(convert[i], To[i]);
            }
            return s;
        }
        public static string convertToUrl(string s)
        {
            string url = "";
            s = s.ToLower();
            if (s.IndexOf(' ') != -1)
            {
                string[] split = new string[s.Split(' ').Length];
                split = s.Split(' ');

                for (int i = 0; i < split.Length; i++)
                {
                    url += convertToUnSign(split[i].Trim());
                    if (i < split.Length - 1)
                        url += "-";
                }
                s = url;
            }
            else
            {
                url += convertToUnSign(s);
            }
            s = url;
            return s;
        }
        public static string limitReview(string s)
        {
            string[] split = new string[s.Split(' ').Length];
            split = s.Split(' ');
            string title = "";
            if (split.Length > 35)
            {
                for (int i = 0; i < 35; i++)
                {
                    title += split[i];
                    title += " ";
                }
                s = title.Trim();
                s += "...";
            }
            return s;
        }
        public static string limitTitle(string s)
        {
            string[] split = new string[s.Split(' ').Length];
            split = s.Split(' ');
            string title = "";
            if (split.Length > 15)
            {
                for (int i = 0; i < 15; i++)
                {
                    title += split[i];
                    title += " ";
                }
                s = title.Trim();
            }
            return s;
        }
    }
}